import request from '@/utils/request';
import { da } from 'element-plus/es/locales.mjs';
import { jwtDecode } from 'jwt-decode'; // 注意是命名导入 // 确保安装了 jwt-decode 库：npm install jwt-decode

export function login(form) {
    return request({
        url: '/user/login',
        method: 'post',
        data: form
    }).then(response => {

        // ✅ 重点检查响应结构
        console.log('接口响应:', response);
        // 从响应中提取 data 数据
        const { data } = response;
        // 输出 data 数据

        // 将 token 存储到本地
        localStorage.setItem('token', data);
        // alert('token: '+ data);

        // 解析 token 获取 subject
        const decodedToken = jwtDecode(data);
        const subject = decodedToken.sub;

        // 将 subject 存储到本地
        localStorage.setItem('id', subject);

        return { data, subject };
    }).catch(error => {
        console.error('Login failed:', error);
        throw error;
    });
}


export function register(form) {
    return request({
        url: '/user/register',
        method: 'post',
        data: form
    }).then(response => {
        console.log('接口响应:', response);
        localStorage.setItem('id', response.data);
        return { response };
    }).catch(error => {
        console.error('register failed:', error);
        throw error;
    });
}


export function registerpatient(form) {
    return request({
        url: '/user/registerPatient',
        method: 'post',
        data: form
    }).then(response => {

        // ✅ 重点检查响应结构
        console.log('接口响应:', response);
        // 从响应中提取 data 数据
        if (response.data !== true) {
            alert('注册失败，请检查输入信息！');
            throw error;
        }
        return { response };
    }).catch(error => {
        console.error('register failed:', error);
        throw error;
    });
}


export function registerdoctor(form) {
    return request({
        url: '/user/registerDoctor',
        method: 'post',
        data: form
    }).then(response => {

        // ✅ 重点检查响应结构
        console.log('接口响应:', response);
        // 从响应中提取 data 数据
        if (response.data !== true) {
            alert('注册失败，请检查输入信息！');
            throw error;
        }
        alert('注册成功！');
        return { response };
    }).catch(error => {
        console.error('register failed:', error);
        throw error;
    });
}

// 新增接口函数
export function selectUser(userId) {
    return request({
        url: '/user/selectUser',
        method: 'post',
        data: { userId }
    }).then(response => {
        console.log('selectUser 接口响应:', response);
        if (response.code === '000') {
            return response;
        } else {
            console.error('selectUser 调用失败:', response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('selectUser 请求失败:', error);
        throw error;
    });
}

export function logOff(phone) {
    return request({
        url: '/user/logOff',
        method: 'post',
        data: { phone: phone },

    }).then(response => {
        console.log('登出成功:', response);
        return { response };
    }).catch(error => {
        console.error('登出失败:', error);
        throw error;
    });
}